package com.itjeffrey.autocode.controller;

import com.google.common.collect.Maps;
import io.github.swagger2markup.GroupBy;
import io.github.swagger2markup.Language;
import io.github.swagger2markup.Swagger2MarkupConfig;
import io.github.swagger2markup.Swagger2MarkupConverter;
import io.github.swagger2markup.builder.Swagger2MarkupConfigBuilder;
import io.github.swagger2markup.markup.builder.MarkupLanguage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang3.StringUtils;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.BufferedOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.URL;
import java.net.URLEncoder;

/**
 * Swagger2测试
 */
@Api(value = "Swagger2测试")
@RestController
@RequestMapping("/swagger/test")
public class Swagger2TestController {

    private static final String swaggerUrl = "http://localhost:9001/swagger-ui.html";

    @ApiOperation(value = "MarkDown接口文档下载")
    @GetMapping("/download/markdown")
    public void dlMD(HttpServletResponse response){
        try {
            //build Swagger2MarkupConfig
            Swagger2MarkupConfig builder = new Swagger2MarkupConfigBuilder()
                    .withMarkupLanguage(MarkupLanguage.MARKDOWN)
                    .withOutputLanguage(Language.ZH)
                    .withPathsGroupedBy(GroupBy.TAGS)
                    .withGeneratedExamples()
                    .withoutInlineSchema()
                    .build();
            //convert markdown docs string
            String mdString = Swagger2MarkupConverter.from(new URL("http://localhost:9001/v2/api-docs"))
                    .withConfig(builder)
                    .build()
                    .toString();
            //response output
            response.reset();
            response.setContentType("application/octet-stream;charset=utf-8");
            response.setCharacterEncoding("utf-8");
            response.setHeader("Content-disposition", "attachment;filename=" + URLEncoder.encode( "acapidocs.md", "utf-8"));
            BufferedOutputStream bos = new BufferedOutputStream(response.getOutputStream());
            bos.write(mdString.getBytes());
            bos.flush();
            bos.close();
        }catch (Exception e){
            e.printStackTrace();
        }
    }


    @ApiOperation(value = "接口文档下载")
    @GetMapping("/download/word")
    public void dlWord(Model model, @ApiParam(value = "资源URL") @RequestParam(required = false) String url, HttpServletResponse response){
        //1.构建model
        String newUrl = StringUtils.defaultIfBlank(url, swaggerUrl);
        model.addAttribute("url", newUrl);
        model.addAttribute("download", 0);
        model.addAllAttributes(Maps.newHashMap());
        //2.返回response
        //TODO
        response.setContentType("application/octet-stream;charset=utf-8");
        response.setCharacterEncoding("utf-8");
        String fileName = "swagger";
        String content = "";
        try (
            OutputStream os = new BufferedOutputStream(response.getOutputStream());
        ){
            response.setHeader("Content-disposition", "attachment;filename=" + URLEncoder.encode(fileName + "" +
                    ".doc", "utf-8"));
            os.write(content.getBytes(), 0, content.getBytes().length);
            os.flush();
        }catch (IOException e){
            e.printStackTrace();
        }
    }

}
